package cn.wenhaha.app.data.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.data.service.SyncPluginService;
import cn.wenhaha.common.bean.entity.Switch;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import cn.wenhaha.common.db.bean.SysSyncPlugin;
import cn.wenhaha.common.exception.ApiMSGException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

/**
 * 同步插件控制器
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-08-29 19:52
 */
@RestController
@RequestMapping("/api/v1/sync/plugin")
public class ApiDataPluginSyncController {

    @Resource
    private SyncPluginService pluginService;

    @Resource
    private SysSyncPlugin syncPlugin;


    /**
     * 添加插件
     * @return
     */
    @PostMapping("add")
    @SaCheckRole("admin")
    public AjaxV1Result<String> add(@RequestParam("file") MultipartFile file) {
        return AjaxV1Result.newSuccess(null , pluginService.addPlugin(file));
    }




    /**
     * 删除插件
     * @return
     */
    @GetMapping("remove")
    @SaCheckRole("admin")
    public AjaxV1Result<String> remove(String id){
        if (id!=null){
            pluginService.remove(id);
        }
        return AjaxV1Result.newSuccess();
    }




    /**
     * 升级或者降级
     * @param file
     * @return
     */
    @PostMapping("update")
    @SaCheckRole("admin")
    public AjaxV1Result<String> update(@RequestParam("file") MultipartFile file) {
        return AjaxV1Result.newSuccess(null,pluginService.update(file));
    }



    /**
     * 切换插件启用状态<br/>
     *
     * @return
     */
    @GetMapping("switch")
    @SaCheckRole("admin")
    public AjaxV1Result<String> switchStatus(String id){
        SysSyncPlugin plugin = Optional.ofNullable(syncPlugin.findById(id))
                .orElseThrow(() -> ApiMSGException.build(7009));
        String status = plugin.getStatus();
        if (StrUtil.equals(status, Switch.ON.getCode().toString())){
            // 关掉
            pluginService.disabled(plugin);
        } else {
            // 开启
            pluginService.enable(plugin);
        }
        return AjaxV1Result.newSuccess();
    }



    /**
     * 插件列表
     * @return
     */
    @PostMapping("list")
    @SaCheckLogin
    public AjaxV1Result<List<SysSyncPlugin>> list(){
        List<SysSyncPlugin> sysSyncPlugins = syncPlugin.find("select * from sys_sync_plugin order by  status DESC");
        return AjaxV1Result.newSuccess(sysSyncPlugins);
    }

    @GetMapping("find/{code}")
    @SaCheckLogin
    public AjaxV1Result<List<SysSyncPlugin>> getSupperSyncPlugin(@PathVariable("code") String code) throws SQLException {
        List<SysSyncPlugin> sysSyncPlugins = syncPlugin
                .find("select * from sys_sync_plugin  where  status=1 and support LIKE ? order by  status DESC",
                        "%" + code + "%");

        return AjaxV1Result.newSuccess(sysSyncPlugins);
    }



}
